package 数据结构和算法.稀疏数组

import scala.collection.mutable.ArrayBuffer

object SparseArr {
  def main(args: Array[String]): Unit = {
    println("-----------展示原始数组-------------")
    val chessMap = Array.ofDim[Int](11, 11);
    chessMap(1)(2) = 1
    chessMap(2)(3) = 2
    for (item <- chessMap) {
      for (item2 <- item) {
        printf("%d\t", item2)
      }
      println
    }
    println("-----------查看压缩数据-------------")
    /**
     * 转换为稀疏数组保存
     * 思路:=>达到压缩效果即可
     */
    val sparseArr = new ArrayBuffer[NOde]
    val ode = NOde(11, 11, 0)
    sparseArr.append(ode)
    for (i <- chessMap.indices) {
      for (j <- chessMap(i).indices) {
        if (chessMap(i)(j) != 0) {
          val node = NOde(i, j, chessMap(i)(j))
          sparseArr.append(node)
        }
      }
    }
    for (node <- sparseArr) {
      printf("%d\t%d\t%d\t", node.row, node.col, node.value)
      println
    }
    println("-----------读取压缩数组-------------")
    /**
     * 读取压缩数据
     */
    val size = sparseArr(0)
    val row = size.row
    val col = size.col
    val chessMap2 = Array.ofDim[Int](row, col)
    sparseArr.tail.foreach(i => chessMap2(i.row)(i.col) = i.value)
    for (item <- chessMap2) {
      for (item2 <- item) {
        printf("%d\t", item2)
      }
      println
    }
  }
}


class NOde(val row: Int, val col: Int, val value: Int) {

}

object NOde {
  def apply(row: Int, col: Int, value: Int): NOde = new NOde(row, col, value)
}
